package com.mao.disruptor.producer;

import com.lmax.disruptor.RingBuffer;
import com.mao.disruptor.entity.LongEvent;

import java.nio.ByteBuffer;

/**
 * 生产者
 */
public class LongEventProducer {
    private RingBuffer<LongEvent> ringBuffers;

    public LongEventProducer(RingBuffer<LongEvent> ringBuffers) {
        this.ringBuffers = ringBuffers;
    }

    public LongEventProducer() {
    }

    public void onData(ByteBuffer byteBuffer) {
        // 获取事件队列的下标位置
        long sequence = ringBuffers.next();
        try {
            // 取出空队列（Event）
            LongEvent longEvent = ringBuffers.get(sequence);
            // 给空队列赋值
            longEvent.setValue(byteBuffer.getLong(0));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("生产者发送数据...");
            //发送数据 --- 放到环形数组中去
            ringBuffers.publish(sequence);
        }

    }
}
